import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/product.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/scroll_refresh_config.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

///
/// 商品分类
///
///
class ProductClassify extends StatefulWidget {
  const ProductClassify({Key? key}) : super(key: key);

  @override
  State<ProductClassify> createState() => _ProductClassifyState();
}

class _ProductClassifyState extends State<ProductClassify> {
  final RefreshController refreshController = RefreshController(initialRefresh: false);

  List<ProductClassifyModel> pageList = [];

  Widget get actions {
    return BrnTextAction(
      '排序',
      iconPressed: () {
        Get.toNamed(RouteGet.productClassifySort);
      },
      themeData: BrnAppBarConfig(
          actionsStyle: BrnTextStyle(
              color: Colors.white, fontSize: Sizes.default16, fontWeight: FontWeight.normal)),
    );
  }

  @override
  void initState() {
    super.initState();
    pageList.add(ProductClassifyModel(name: '石磨肠粉', describe: '美味好吃上瘾', id: 1));
    pageList.add(ProductClassifyModel(name: '石磨肠粉', describe: '美味好吃上瘾', id: 2));
    pageList.add(ProductClassifyModel(name: '石磨肠粉', describe: '美味好吃上瘾', id: 3));
    pageList.add(ProductClassifyModel(name: '石磨肠粉', describe: '美味好吃上瘾', id: 4));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('商品分类', actions: actions),
      body: ScrollRefreshConfig(
        refreshController: refreshController,
        onRefresh: onRefresh,
        onLoading: onLoading,
        child: listViewBuilder(),
      ),
      bottomNavigationBar: SafeArea(
        top: false,
        child: Container(
            height: Sizes.default60,
            decoration: const BoxDecoration(color: Colors.white),
            alignment: Alignment.center,
            child: TextButton(
              onPressed: () {
                Get.toNamed(RouteGet.productClassifyAddEdit);
              },
              child: Text.rich(TextSpan(children: [
                WidgetSpan(
                    child:
                        Icon(AntdIcons.plusCircleFill, color: ColorsUtils.rgba(255, 110, 83, 1))),
                TextSpan(
                    text: '添加分类',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default15, color: ColorsUtils.color333))
              ])),
            )),
      ),
    );
  }

  Widget listViewBuilder() {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        var item = pageList[index];
        return InkWell(
          onTap: () {
            Get.toNamed(RouteGet.productClassifyAddEdit, arguments: item);
          },
          child: Container(
            margin: const EdgeInsets.only(
                top: Sizes.default15, left: Sizes.default15, right: Sizes.default15),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
            padding: const EdgeInsets.symmetric(vertical: Sizes.default10),
            child: Stack(
              children: [
                Column(
                  children: [
                    Container(
                      height: Sizes.default40,
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
                      child: Text(
                        '名称：${item.name}',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                      ),
                    ),
                    Container(
                      height: Sizes.default30,
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
                      child: Text(
                        '分类描述：${item.describe}',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                      ),
                    ),
                    Container(
                      height: Sizes.default40,
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
                      child: Text(
                        '是否可用：${item.available ? '是' : '否'}',
                        style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                      ),
                    ),
                  ],
                ),
                Positioned(
                    top: 0,
                    right: Sizes.default20,
                    bottom: 0,
                    child: Center(
                      child: BrunoTools.getAssetSizeImage(
                          BrnAsset.iconRightArrow, Sizes.default16, Sizes.default16),
                    ))
              ],
            ),
          ),
        );
      },
      itemCount: pageList.length,
      shrinkWrap: true,
      itemExtent: 145,
      padding: EdgeInsets.zero,
    );
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
    setState(() {});
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 10) {
      return refreshController.loadNoData();
    }

    /// 添加
    refreshController.loadComplete();
    setState(() {
      pageList.add(ProductClassifyModel(name: '石磨肠粉', describe: '美味好吃上瘾', id: pageList.length + 1));
    });
  }
}
